/**
 * 页面加载完成处理
 */
+(function(e){
	var dis_block="block";
	var dis_none="none";
	var dictVue=new Vue({
		el:"#dictContainer",
		data:function(){
			var checkDictTypeCode=function(rule, value, callback){ // 保存时验证用户名是否可以
	        	var url=ctx+"/welcome/dict/type/code/own";
	        	$.ajax({url:url,type:"POST",dataType:"json",data:{typeCode:value},success:function(data){
	        		if(data.success==true)
	        		{
	        			callback();
	        		}
	        		else
	        		{
	        			callback(new Error('该字典类型编码已经被使用'));
	        		}
	        	}});
	        };
	        var checkDictValueCode=function(rule, value, callback){ // 保存时验证用户名是否可以
	        	var url=ctx+"/welcome/dict/value/code/own";
	        	$.ajax({url:url,type:"POST",dataType:"json",data:{typeCode:this.selectDict.code,valueCode:value},success:function(data){
	        		if(data.success==true)
	        		{
	        			callback();
	        		}
	        		else
	        		{
	        			callback(new Error('该字典值编码已经被使用'));
	        		}
	        	}});
	        };
			return {
				loading:false,
				loadingTxt:$.admin.loadingTxt,
				tableHeight:pageContentHeight-80,
				dictTypeDisplay:dis_block,
				dictValueDisplay:dis_none,
				formLabelWidth:"100px",
				
				currentPage:1,
				defaultPageSize:defaultPageSize,
				pageSize:defaultPageSize,
				totalCount:0,
				selectDictName:"",
				selectDict:null,
				dictTypeForm:{
					name:""
				},
				
				editDiag:{
					title:"新增字典",
					show:false,
					codeDisable:true,
					codeProp:"code",
					type:1,
					form:{
						id:"",
						code:"",
						name:""
					},
					rules:{
						code:[
			        		{ required: true, message: '字典类型编码不能为空', trigger: 'blur' },
			        		{ validator:checkDictTypeCode, trigger: 'blur' }
			        	],
			        	name:[
			        		{ required: true, message: '字典类型名称不能为空', trigger: 'blur' }
			        	]
					}
				},
				
				dictTypes:[],
				dictValues:[],
				
				editDiagDv:{
					title:"新增字典值",
					show:false,
					codeDisable:true,
					type:1,
					codeProp:"code",
					form:{
						id:"",
						code:"",
						name:"",
						dictTypeId:""
					},
					rules:{
						code:[
			        		{ required: true, message: '字典值编码不能为空', trigger: 'blur' },
			        		{ validator:checkDictValueCode, trigger: 'blur' }
			        	],
			        	name:[
			        		{ required: true, message: '字典值名称不能为空', trigger: 'blur' }
			        	]
					}
				}
			}
		},
		methods:{
		  qryDictType:function(inited)
		   {
			   if(1!=inited)
			   {
				 this.currentPage=1;
				 this.totalCount=0;
			   }
			   var $this=this;
			   var url=ctx+"/welcome/dict/type/qry";
			   this.loading=true;
			   var params={name:this.dictTypeForm.name,pageIndex:this.currentPage-1,pageSize:this.pageSize};
			   $.ajax({url:url,type:"POST",dataType:"json",data:params,success:function(data){
				   if(data && data.success==true)
				   {
					   var data=data.data;
					   $this.dictTypes=data.content;
					   $this.totalCount=data.totalElements;
				   }
				   $this.loading=false;
			   }});
		   },
		   handleDictTypeCurrentChange(row) //字典选择改变处理
		   {
			 this.selectDict=row;
			 this.selectDictName=row.name;
			 this.editDiagDv.form.dictTypeId=row.id;
		   },
	       handleCurrentChange:function(val) { //分页条分页发生改变
	        	this.currentPage=val;
	        	this.qryDictType(1);
	        },
	       openAddPage:function(type,event){  //打开新增/修改页面
				this.editDiag.form;
				this.editDiag.type=type;
				this.editDiag.show=true;
				if(1==type)
				{
					this.editDiag.title="新增字典";
					this.editDiag.form.id="";
				}
				else if(2==type)
				{
					this.editDiag.title="修改字典";
					var url=ctx+"/welcome/dict/type/id/one";
					var $this=this;
					var $t=$(event.currentTarget);
					var params={typeId:$t.attr("pid")};
					$.ajax({url:url,type:"POST",dataType:"json",data:params,success:function(data){
						$this.editDiag.form=data.data;
					}});
				}
			},
			onCloseDictTypeDiag:function(event) //关闭字典类型页面
			{
				var $dictTypeDialogForm =this.$refs["dictTypeDialogForm"];
				if($dictTypeDialogForm)
				{
					$dictTypeDialogForm.resetFields();
				}
			},
			saveDictType:function(event)//保存字典类型页面
			{
				var $this=this;
				this.$refs["dictTypeDialogForm"].validate(function(valid){
					if(valid==true)
					{
						var msg="保存成功";
						if($this.editDiag.type==1)
						{
							msg="保存成功";
						}
						else
						{
							msg="修改成功";
						}
						var url=ctx+"/admin/dict/type/save";
						var params=$this.editDiag.form;
						$.ajax({url:url,type:"POST",dataType:"json",data:params,success:function(data){
							var data=data.data;
							$this.editDiag.show=false;
							$this.qryDictType();
							$this.$message({
						          message: msg,
						          type: 'info'
						      });
						}});
					}
				});
			},
		   del:function(event)  //删除字典类型
			{
				var $this=this;
				var $t=$(event.currentTarget);
				this.$confirm('确定要删除?', '提示', {
		          confirmButtonText: '确定',
		          cancelButtonText: '取消',
		          type: 'warning'
		        }).then(function(){
		        	var url=ctx+"/admin/dict/type/del";
					var params={typeId:$t.attr("pid")}
					$.ajax({url:url,type:"POST",dataType:"json",data:params,success:function(data){
						if(data.success==true)
						{
							$this.qryDictType();
							$this.$message({
					            type: 'info',
					            message: '删除成功！'
					         }); 
						}
						else
						{
							$this.qryDictType();
							$this.$message({
					            type: 'warning',
					            message: data.msg
					         });
						}
					}});
		        }).catch(function(){
		          $this.$message({
		            type: 'info',
		            message: '已取消删除'
		          });          
		        });
			},
			openDictValueWin:function(event){ //打开字典值界面
				var $t=$(event.currentTarget);
				var typeId=$t.attr("pid");
				qryTypeValues(typeId);
				this.dictTypeDisplay=dis_none;
				this.dictValueDisplay=dis_block;
	        },
	        
	        //字典值列表
	        qryTypeValues:function(dictTypeId)
	        {
	           var $this=this;
			   var url=ctx+"/welcome/dict/type/id/values";
			   this.loading=true;
			   var params={typeId:dictTypeId};
			   $.ajax({url:url,type:"POST",dataType:"json",data:{},success:function(data){
				   $this.dictValues=data.data;
			   }});
	        },
	        openAddDictValuePage:function(type,event){  //打开字典值新增/修改页面
				this.editDiagDv.type=type;
				this.editDiagDv.show=true;
				if(1==type)
				{
					this.editDiagDv.title="新增字典值";
					this.editDiagDv.form.id="";
				}
				else if(2==type)
				{
					this.editDiagDv.title="修改字典值";
					var url=ctx+"/welcome/dict/value/id/one";
					var $this=this;
					var $t=$(event.currentTarget);
					var params={valueId:$t.attr("pid")};
					$.ajax({url:url,type:"POST",dataType:"json",data:params,success:function(data){
						$this.editDiagDv.form=data.data;
					}});
				}
			},
			onCloseDictValueDiag:function(event) //关闭字典值页面
			{
				var $dictValueDialogForm =this.$refs["dictValueDialogForm"];
				if($dictTypeDialogForm)
				{
					$dictTypeDialogForm.resetFields();
				}
			},
			saveDictValue:function(event)//保存字典类型页面
			{
				var $this=this;
				this.$refs["dictValueDialogForm"].validate(function(valid){
					if(valid==true)
					{
						var msg="保存成功";
						if($this.editDiag.type==1)
						{
							msg="保存成功";
						}
						else
						{
							msg="修改成功";
						}
						var url=ctx+"/admin/dict/value/save";
						var params=$this.editDiag.form;
						params.dictTypeId=$this.selectDict.id;
						$.ajax({url:url,type:"POST",dataType:"json",data:params,success:function(data){
							var data=data.data;
							$this.editDiag.show=false;
							$this.qryDictType();
							$this.$message({
						          message: msg,
						          type: 'info'
						      });
						}});
					}
				});
			},
		   delDictValue:function(event)  //删除字典类型
			{
				var $this=this;
				var $t=$(event.currentTarget);
				this.$confirm('确定要删除?', '提示', {
		          confirmButtonText: '确定',
		          cancelButtonText: '取消',
		          type: 'warning'
		        }).then(function(){
		        	var url=ctx+"/admin/dict/type/del";
					var params={typeId:$t.attr("pid")}
					$.ajax({url:url,type:"POST",dataType:"json",data:params,success:function(data){
						if(data.success==true)
						{
							$this.qryDictType();
							$this.$message({
					            type: 'info',
					            message: '删除成功！'
					         }); 
						}
						else
						{
							$this.qryDictType();
							$this.$message({
					            type: 'warning',
					            message: data.msg
					         });
						}
					}});
		        }).catch(function(){
		          $this.$message({
		            type: 'info',
		            message: '已取消删除'
		          });          
		        });
			}
		}
	});
	dictVue.qryDictType();
})();